package javaeSE;


import java.util.Random;

//面试题：多态是编译时行为还是运行时行为？  运行时行为
//证明如下：因为程序只能在程序运行的时候才能决定调用哪个对象的方法
class Animals  {
 
	protected void eat() {
		System.out.println("animal eat food");
	}
}

class Cats  extends Animals  {
 
	protected void eat() {
		System.out.println("cat eat fish");
	}
}

class Dogs  extends Animals  {
 
	public void eat() {
		System.out.println("Dog eat bone");

	}

}

class Sheep  extends Animals {
 

	public void eat() {
		System.out.println("Sheep eat grass");

	}

 
}

// 证明多态是  运行时行为
public class VariableTest {
	public static Animals  getInstance(int key) {
		switch (key) {
		case 0:
			return new Cats ();
		case 1:
			return new Dogs ();
		default:
			return new Sheep ();
		}

	}

	public static void main(String[] args) {
		int key = new Random().nextInt(3);

		System.out.println(key);

		Animals  animal = getInstance(key);
		
		animal.eat();
		 
	}

}
